package com.example.administrator.sqliteopenhelper;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnOpen;
    private Button btnInsert;
    private StuOpenHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOpen = findViewById(R.id.btn_open);
        btnInsert = findViewById(R.id.btn_insert);

        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                *  context   name数据库名  factory一般为null  version版本号
                * */
                helper = new StuOpenHelper(MainActivity.this,
                        "stu.db", null, 2);
                helper.getWritableDatabase();
            }
        });

        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = helper.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put("stuno", "17001");
                values.put("name", "Sun");
                values.put("age", 22);

                ContentValues values2 = new ContentValues();
                values2.put("stuno", "17002");
                values2.put("name", "Lina");
                values2.put("age", 30);

                db.insert("student",null,values);
                db.insert("student",null,values2);
            }
        });
    }
}
